package com.cskaoyan.service.admin.promotion;
/*
 * @Auther:王贲
 * @Date:2022/9/6
 * @Description:
 * @VERSON:1.8
 */

import com.cskaoyan.bean.MarketAd;
import com.cskaoyan.bean.MarketAdExample;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.admin.promotion.AdminAdInsertVO;
import com.cskaoyan.mapper.MarketAdMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AdminAdServiceImpl implements AdminAdService {

    @Autowired
    MarketAdMapper marketAdMapper;

    /**
     * @return com.cskaoyan.bean.common.CommonData
     * @description : TODO 广告页面显示的实现方法
     * @author wang'ben
     * @date 2022/9/6 20:25
     */
    @Override
    public CommonData selectAdInfo(BasePageInfo basePageInfo, String name, String content) {
        // 添加分页逻辑与查询逻辑
        // 1. 开启分页
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());
        MarketAdExample marketAdExample = new MarketAdExample();
        MarketAdExample.Criteria criteria = marketAdExample.createCriteria();
        marketAdExample.setOrderByClause(basePageInfo.getSort() + " " + basePageInfo.getOrder());
        criteria.andDeletedEqualTo(false);
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (!StringUtils.isEmpty(content)) {
            criteria.andContentLike("%" + content + "%");
        }

        // 1. 首先应该将数据库中的数据全部显示并封装进List<MarketAd>对象中
        List<MarketAd> list = marketAdMapper.selectByExample(marketAdExample);
        Integer total = (int) marketAdMapper.countByExample(null);
        // 2. 将获得的marketAdList数组和basePageInfo放入CommonData对象中
        CommonData commonData = new CommonData<>();
        commonData.setTotal(total);
        commonData.setPages(basePageInfo.getPage());
        commonData.setLimit(basePageInfo.getLimit());
        commonData.setPage(basePageInfo.getPage());
        commonData.setList(list);
        return commonData;
    }

    /**
     * @description : TODO 逻辑删除数据库中的广告资料
     * @return void
     * @author wang'ben
     * @date 2022/9/7 16:39
     */
    @Override
    public void deleteAdInfo(MarketAd marketAd) {
        //1.直接从得到的参数中修改deleteState的值为true
        marketAd.setDeleted(true);
        marketAdMapper.updateByPrimaryKeySelective(marketAd);
    }

    /**
     * @return com.cskaoyan.bean.MarketAd
     * @description : TODO 广告添加的实现方法
     * @author wang'ben
     * @date 2022/9/7 17:33
     */
    @Override
    public void insert(MarketAd marketAd) {
        marketAd.setAddTime(new Date());
        marketAd.setUpdateTime(new Date());
       marketAdMapper.insertSelective(marketAd);
    }

    /**
     * @description : TODO 广告的更新实现
     * @return void
     * @author wang'ben
     * @date 2022/9/7 19:44
     */
    @Override
    public void update(MarketAd marketAd) {
        marketAd.setUpdateTime(new Date());
        marketAdMapper.updateByPrimaryKeySelective(marketAd);
    }


}
